/**
 * @author MIAO
 * Created on 2011-10-31
 * For more information, please visit:ismiao.com
 */
package com.ismiao.spring.jdbcTemplate;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class TestTransaction {
	public static void main(String[] args) throws Exception {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection("jdbc:mysql://localhost:3306/test","root","root");
			Statement stat = conn.createStatement();
			conn.setAutoCommit(false);// 设置为不要自动提交
			String sql1 = "UPDATE1";
			String sql2 = "UPDATE2";
			stat.executeUpdate(sql1);
			stat.executeUpdate(sql2);
			conn.commit(); // 提交以上操作
			System.out.println("保证两条语句全部执行");
		} catch (Exception ex) {
			conn.rollback(); // 回滚
			System.out.println("回滚，保证两条语句全部不执行");
		} finally {
			conn.close();
			System.out.println("关闭数据库连接！");
		}
	}
}
